class Solution {
public:
    int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
        int ans = 0, i = 0;
        priority_queue<int> pq;
        bool canReach = startFuel >= target;

        while (not canReach) {
            while (i < stations.size() and stations[i][0] <= startFuel) {
                pq.emplace(stations[i][1]);
                ++i;
            }
            if (pq.empty()) {
                return -1;
            }
            // 加油
            startFuel += pq.top();
            pq.pop();
            ++ans;
            // 更新是否能够到达目的地
            canReach = startFuel >= target;
        }
        return ans;
    }
};
